﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using WebUI.BL.Helper;

namespace WebUI
{
    /// <summary>
    /// Summary description for DisplayImage
    /// </summary>
    public class DisplayImage : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.Clear();
            context.Response.ContentType = "image/jpeg";
            if(context.Request.QueryString["ImgId"]!=null)
            {
                int imgId = 0;
                imgId = Convert.ToInt16(context.Request.QueryString["ImgId"]);
                MemoryStream memoryStream = new MemoryStream(GetImageFromDB(imgId), false);
                System.Drawing.Image imgFromDataBase = System.Drawing.Image.FromStream(memoryStream);
                imgFromDataBase.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);     
            }
            
        }

        private byte[] GetImageFromDB(int imgId)
        {
            var uploadFileBL = new UploadFileBL();
            return uploadFileBL.GetImgById(imgId);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}